﻿using System;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using SaleProgram.Reports;

namespace SaleProgram
{
    public partial class MainForm : Form
    {
        private DataSet _FirstDataSet;
        private DataTable _SingleContentDatatable;
        private DataTable _NomberDatatable;
        public MainForm()
        {
            InitializeComponent();
            this.Visible = false;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            LoginForm loginForm = new LoginForm();
        rep: DialogResult result = loginForm.ShowDialog(this);
            if (DialogResult.OK == result)
            {
                this.Visible = true;
                _FirstDataSet = DataAccess.GetAllData();
                _SingleContentDatatable = _FirstDataSet.Tables["singleContentDatatable"];
                _SingleContentDatatable.Rows[0]["LoginName"] = loginForm.ResultLoginName;

                _NomberDatatable = _FirstDataSet.Tables["nomberDataTable"];
                this.nombreGridControl.DataSource = _NomberDatatable;

                InitUIData();
            }
            else if (result == DialogResult.No)
                goto rep;
            else
                this.Close();
        }

        private void InitUIData()
        {
            tbTeamID.DataBindings.Add("EditValue", _SingleContentDatatable, "TeamID");
            tbIssuedDate.DataBindings.Add("EditValue", _SingleContentDatatable, "IssuedDate");
            lblLoginName.DataBindings.Add("Text", _SingleContentDatatable, "LoginName");

            tbGuestName.DataBindings.Add("EditValue", _SingleContentDatatable, "GuestName");
            tbGuestAddress.DataBindings.Add("EditValue", _SingleContentDatatable, "GuestAddress");
            tbVoyageName.DataBindings.Add("EditValue", _SingleContentDatatable, "VoyageName");
            tbStartDate.DataBindings.Add("EditValue", _SingleContentDatatable, "StartDate");
            tbEndDate.DataBindings.Add("EditValue", _SingleContentDatatable, "EndDate");
            tbVoyageType.DataBindings.Add("EditValue", _SingleContentDatatable, "VoyageType");
            tbFlightTrainInfo.DataBindings.Add("EditValue", _SingleContentDatatable, "FlightTrainInfo");
            tbPersonalNum.DataBindings.Add("EditValue", _SingleContentDatatable, "PersonalNum");
            tbPriceDetail.DataBindings.Add("EditValue", _SingleContentDatatable, "PriceDetail");
            tbPriceTotal.DataBindings.Add("EditValue", _SingleContentDatatable, "PriceTotal");
            tbFirstPayMentDate.DataBindings.Add("EditValue", _SingleContentDatatable, "FirstPayMentDate");
            tbSecondPayMentDate.DataBindings.Add("EditValue", _SingleContentDatatable, "SecondPayMentDate");
            tbFirstPayMentType.DataBindings.Add("EditValue", _SingleContentDatatable, "FirstPayMentType");
            tbSecondPayMentType.DataBindings.Add("EditValue", _SingleContentDatatable, "SecondPayMentType");
            tbFirstPayMentAmount.DataBindings.Add("EditValue", _SingleContentDatatable, "FirstPayMentAmount");
            tbSecondPayMentAmount.DataBindings.Add("EditValue", _SingleContentDatatable, "SecondPayMentAmount");
            tbTotalPayMentAmount.DataBindings.Add("EditValue", _SingleContentDatatable, "TotalPayMentAmount");
            tbRemainPayMentAmount.DataBindings.Add("EditValue", _SingleContentDatatable, "RemainPayMentAmount");
            tbExistItem.DataBindings.Add("EditValue", _SingleContentDatatable, "ExistItem");
            tbNoExistItem.DataBindings.Add("EditValue", _SingleContentDatatable, "NoExistItem");

            lblIssuedDate.DataBindings.Add("Text", _SingleContentDatatable, "IssuedDate");
            lblLoginName1.DataBindings.Add("Text", _SingleContentDatatable, "LoginName");//
            //lblGuestName.DataBindings.Add("Text", _SingleContentDatatable, "GuestName");

            tbHotelInformation.DataBindings.Add("Text", _SingleContentDatatable, "HotelInformation");
            tbTourTravel.DataBindings.Add("Text", _SingleContentDatatable, "ToursTravel");

            tbTotalCurrencySymbol.DataBindings.Add("EditValue", _SingleContentDatatable, "TotalCurrencySymbol");
            tbFirstPayMentAmountCurrencySymbol.DataBindings.Add("EditValue", _SingleContentDatatable, "FirstPayMentAmountCurrencySymbol");
            tbSecondPayMentAmountCurrencySymbol.DataBindings.Add("EditValue", _SingleContentDatatable, "SecondPayMentAmountCurrencySymbol");
            tbTotalPayCurrencySymbol.DataBindings.Add("EditValue", _SingleContentDatatable, "TotalPayCurrencySymbol");
            tbRemainPayCurrencySymbol.DataBindings.Add("EditValue", _SingleContentDatatable, "RemainPayCurrencySymbol");
            tbRemainPayDateTime.DataBindings.Add("EditValue", _SingleContentDatatable, "RemainPayDateTime");

        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            Report1 rpt = new Report1();
            rpt.DataSource = null;
            rpt.DataSource = _FirstDataSet;
            rpt.ShowPreview();
        }
        private void btnCreatePdf_Click(object sender, EventArgs e)
        {

            Report1 rpt1 = new Report1();
            rpt1.DataSource = null;
            rpt1.DataSource = _FirstDataSet;

            if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {

                rpt1.ExportToPdf(saveFileDialog1.FileName);
                if (MessageBox.Show("保存完毕,打开文件吗？", "文件保存", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    Process.Start(saveFileDialog1.FileName);
            }
        }


        private void btnAddRow_Click(object sender, EventArgs e)
        {
            DataRow row = DataAccess.FirstPageDataset.Tables["nomberDatatable"].NewRow();
            row["Titre"] = "";
            row["Nom"] = "";
            row["Prenom"] = "";
            row["passeport"] = "";
            DataAccess.FirstPageDataset.Tables["nomberDatatable"].Rows.Add(row);
        }

        private void btnDeleteRow_Click(object sender, EventArgs e)
        {
            if (nombreGridView.FocusedRowHandle >= 0)
                DataAccess.FirstPageDataset.Tables["nomberDatatable"].Rows.RemoveAt(nombreGridView.FocusedRowHandle);
        }

        private void tbGuestName_EditValueChanged(object sender, EventArgs e)
        {
            lblGuestName.Text = tbGuestName.Text;
        }


    }
}